﻿Imports System.Data.OleDb

Public Class frmXemDonHang
    Public IDDonHangBan As Integer = 0
    Public FormSender As String = ""
    Private KhachHangID As Integer
    Private Sub frmXemDonHang_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Me.BackColor = Color.FromArgb(194, 217, 247)
            comboTien_Load()
            GenerateOrderNumber()
            InitOrder()
            DonHangBan_Load()
            If FormSender = "TheoDoiDonHang" Then
                cmdChuyenPhieuKho.Enabled = True
            Else
                cmdChuyenPhieuKho.Enabled = False
            End If
        Catch ex As Exception
            ShowError(ex)
        End Try
    End Sub
    Private Sub comboTien_Load()
        Try
            Dim Tien As New List(Of String)
            Tien.Add("VND")
            Tien.Add("USD")
            Tien.Add("SGD")
            comboTien.DataSource = Tien
            comboTien.SelectedIndex = 0
            'set index
        Catch ex As Exception

        End Try
    End Sub
    Protected Function InitOrderTable() As DataTable
        Dim dtTable As DataTable = New DataTable()
        Try
            'RowIndex 
            Dim RowIndex As DataColumn = New DataColumn("RowIndex")
            RowIndex.DataType = System.Type.GetType("System.Int32")
            dtTable.Columns.Add(RowIndex)

            Dim IDSanPham As DataColumn = New DataColumn("IDSanPham")
            IDSanPham.DataType = System.Type.GetType("System.Int32")
            dtTable.Columns.Add(IDSanPham)

            Dim MaSanPham As DataColumn = New DataColumn("MaSanPham")
            MaSanPham.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(MaSanPham)

            Dim TenSanPham As DataColumn = New DataColumn("TenSanPham")
            TenSanPham.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TenSanPham)

            Dim TenQuyCach As DataColumn = New DataColumn("TenQuyCach")
            TenQuyCach.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(TenQuyCach)

            Dim SoLuong As DataColumn = New DataColumn("SoLuong")
            SoLuong.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(SoLuong)

            Dim DonGia As DataColumn = New DataColumn("DonGia")
            DonGia.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(DonGia)

            Dim IsManyPrice As DataColumn = New DataColumn("IsManyPrice")
            IsManyPrice.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(IsManyPrice)

            Dim ThanhTien As DataColumn = New DataColumn("ThanhTien")
            ThanhTien.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(ThanhTien)

            Dim LoaiPhi As DataColumn = New DataColumn("LoaiPhi")
            LoaiPhi.DataType = System.Type.GetType("System.String")
            dtTable.Columns.Add(LoaiPhi)

            Return dtTable
        Catch ex As Exception
            Throw
        End Try
    End Function
    Private Sub GenerateOrderNumber()
        Try
            Dim dtNoOrder As New DataTable
            Dim NoOrder As DataRow
            dtNoOrder.Columns.Add("STT")
            For i As Integer = 0 To 11
                NoOrder = dtNoOrder.NewRow
                NoOrder.Item("STT") = i + 1
                dtNoOrder.Rows.Add(NoOrder)
                dtNoOrder.AcceptChanges()
            Next
            dgvNoOrder.DataSource = dtNoOrder
            dgvNoOrder.ClearSelection()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub InitOrder()
        Try
            'dtSanPhamHD = CreateSanPhamHopDong()
            Dim dtOrder As New DataTable
            dtOrder = InitOrderTable()
            Dim newRow As DataRow
            For i As Integer = 0 To 11
                newRow = dtOrder.NewRow
                newRow.Item("RowIndex") = i + 1
                newRow.Item("IDSanPham") = 0
                newRow.Item("MaSanPham") = ""
                newRow.Item("TenSanPham") = ""
                newRow.Item("TenQuyCach") = ""
                newRow.Item("SoLuong") = ""
                newRow.Item("DonGia") = ""
                newRow.Item("ThanhTien") = ""
                newRow.Item("LoaiPhi") = ""
                newRow.Item("IsManyPrice") = ""
                dtOrder.Rows.Add(newRow)
            Next
            dgvSanPhamHopdong_Tab1.AutoGenerateColumns = False
            dgvSanPhamHopdong_Tab1.DataSource = dtOrder
        Catch ex As Exception
            Throw
        End Try
    End Sub
    Private dtChiTietDonHang As DataTable
    Private Sub DonHangBan_Load()
        Try
            Dim dbConn As New OleDb.OleDbConnection
            Dim dataAccess As New DataAccess
            Dim dbCommand As OleDb.OleDbCommand
            dataAccess.ConnectDatabase_DBAccess(dbConn)
            'Trạng Thái Đơn Hàng 
            Dim TrangThai As String
            dbCommand = New OleDbCommand("spDonHangBan_TrangThai", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@IDDonHangBan", OleDbType.Integer).Value = IDDonHangBan
            TrangThai = dbCommand.ExecuteScalar()
            If TrangThai = "Đã duyệt" Then
                cbxTruKho.Checked = True
            ElseIf TrangThai = "Chưa duyệt" Then
                cbxTruKho.Checked = False
                'DaTruKho = False
            End If
            '
            'Thong Tin Don Hang spDonHangBan_DonHang
            Dim dtDonHang As New DataTable
            dbCommand = New OleDbCommand("spDonHangBan_DonHang", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@IDDonHangBan", OleDbType.VarWChar).Value = IDDonHangBan
            Dim adapter As OleDb.OleDbDataAdapter
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtDonHang)
            'Thong Tin Khach Hang
            KhachHangID = dtDonHang.Rows(0)("KhachHangID")
            txtHoTenNguoiMua.Text = dtDonHang.Rows(0)("TenNguoiMua")
            txtTenKhachHang.Text = dtDonHang.Rows(0)("TenKhachHang")
            txtDiaChi.Text = dtDonHang.Rows(0)("DiaChi")
            txtMaSoThue.Text = dtDonHang.Rows(0)("MaSoThue")
            dtpNgayHopDong.Value = dtDonHang.Rows(0)("NgayBan")
            txtMaKhachHang.Text = dtDonHang.Rows(0)("CodeKhachHang")
            lblKhachHangId.Text = dtDonHang.Rows(0)("KhachHangId")
            txtHinhThucThanhToan.Text = dtDonHang.Rows(0)("HinhThucThanhToan")
            txtSo.Text = dtDonHang.Rows(0)("So")
            cbxNoPrintMoney.Checked = IIf(dtDonHang.Rows(0)("HoaDonTang") = 1, True, False)
            txtPhiVanChuyen.Value = dtDonHang.Rows(0)("PhiVanChuyen")
            txtGhiChu.Text = IIf(IsDBNull(dtDonHang.Rows(0)("GhiChu")), "", dtDonHang.Rows(0)("GhiChu"))
            'tien
            If dtDonHang.Rows(0)("Tien") = "VND" Then
                comboTien.SelectedIndex = 0
            ElseIf dtDonHang.Rows(0)("Tien") = "USD" Then
                comboTien.SelectedIndex = 1
            Else : dtDonHang.Rows(0)("Tien") = "SGD"
                comboTien.SelectedIndex = 2
            End If

            'Kiem Tra Tiep Thi
            If dtDonHang.Rows(0)("HinhThucTiepThi") = "Đại lý" Then
                rbtDaiLy.Checked = True
                rbtNhanVienTiepThi.Checked = False
                txtDaiLy.Text = dtDonHang.Rows(0)("NhanVienTiepThi")
                lblDaiLyId.Text = dtDonHang.Rows(0)("NhanVienTiepThiId")
            Else
                rbtDaiLy.Checked = False
                rbtNhanVienTiepThi.Checked = True
                cbxNhanVienTT.SelectedValue = dtDonHang.Rows(0)("NhanVienTiepThiId")
            End If
            txtVat.Text = dtDonHang.Rows(0)("ThueVat")
            'Thong Tin Chi Tiet Don Hang spDonHangBan_ChiTietDonHang
            'khoi tao lai
            Dim dtSanPhamHD As New DataTable
            dtSanPhamHD = InitOrderTable()
            Dim newRow As DataRow
            dtChiTietDonHang = New DataTable
            dbCommand = New OleDbCommand("spDonHangBan_ChiTietDonHang", dbConn)
            dbCommand.CommandType = CommandType.StoredProcedure
            dbCommand.Parameters.Add("@IDDonHangBan", OleDbType.Integer).Value = IDDonHangBan
            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            adapter.Fill(dtChiTietDonHang)
            For i As Integer = 0 To dtChiTietDonHang.Rows.Count - 1
                newRow = dtSanPhamHD.NewRow
                newRow.Item("IDSanPham") = dtChiTietDonHang.Rows(i)("IdSanPham")
                newRow.Item("MaSanPham") = dtChiTietDonHang.Rows(i)("MaSanPham")
                newRow.Item("TenSanPham") = dtChiTietDonHang.Rows(i)("TenSanPham")
                newRow.Item("TenQuyCach") = dtChiTietDonHang.Rows(i)("TenQuyCach")
                newRow.Item("SoLuong") = dtChiTietDonHang.Rows(i)("SoLuong")
                newRow.Item("DonGia") = dtChiTietDonHang.Rows(i)("DonGia")
                newRow.Item("ThanhTien") = dtChiTietDonHang.Rows(i)("ThanhTien")
                newRow.Item("LoaiPhi") = dtChiTietDonHang.Rows(i)("LoaiPhi")
                newRow.Item("IsManyPrice") = dtChiTietDonHang.Rows(i)("IsManyPrice")
                dtSanPhamHD.Rows.Add(newRow)
            Next
            'Phat Sinh Dong Trang
            Dim iRowEmpty As Integer = 12 - dtChiTietDonHang.Rows.Count - 1
            For i As Integer = 0 To iRowEmpty
                newRow = dtSanPhamHD.NewRow
                newRow.Item("IDSanPham") = 0
                newRow.Item("MaSanPham") = ""
                newRow.Item("TenSanPham") = ""
                newRow.Item("TenQuyCach") = ""
                newRow.Item("SoLuong") = ""
                newRow.Item("DonGia") = ""
                newRow.Item("ThanhTien") = ""
                newRow.Item("LoaiPhi") = ""
                dtSanPhamHD.Rows.Add(newRow)
            Next
            dtSanPhamHD.DefaultView.Sort = "LoaiPhi DESC"
            dgvSanPhamHopdong_Tab1.DataSource = dtSanPhamHD
            'Dim listPrice As New List(Of String)
            'Dim comboxPrice As New DataGridViewComboBoxCell
            'For i As Integer = 0 To 11
            '    dgvSanPhamHopdong_Tab1("IDSanPham", i).Value = dtSanPhamHD.Rows(i)("IDSanPham")
            '    dgvSanPhamHopdong_Tab1("MaHang", i).Value = dtSanPhamHD.Rows(i)("MaSanPham")
            '    dgvSanPhamHopdong_Tab1("TenHang", i).Value = dtSanPhamHD.Rows(i)("TenSanPham")
            '    dgvSanPhamHopdong_Tab1("DonViTinh", i).Value = dtSanPhamHD.Rows(i)("TenQuyCach")
            '    If dgvSanPhamHopdong_Tab1("MaHang", i).Value <> "" Then
            '        If dtSanPhamHD.Rows(i)("IsManyPrice") = "1" Then
            '            'Nhieu gia
            '            'Lay Het Gia Len
            '            listPrice = New List(Of String)
            '            Dim dtListPrice As New DataTable
            '            dbCommand = New OleDbCommand("spGetPriceByCustomerAndID", dbConn)
            '            dbCommand.CommandType = CommandType.StoredProcedure
            '            dbCommand.Parameters.Add("@KhachHangId", OleDbType.Integer).Value = lblKhachHangId.Text.Trim
            '            dbCommand.Parameters.Add("@IDSanPham", OleDbType.VarWChar).Value = dtSanPhamHD.Rows(i)("IDSanPham")
            '            adapter = New OleDb.OleDbDataAdapter(dbCommand)
            '            adapter.Fill(dtListPrice)
            '            For j As Integer = 0 To dtListPrice.Rows.Count - 1
            '                listPrice.Add(dtListPrice.Rows(j)("DonGia"))
            '            Next
            '            comboxPrice = CType(dgvSanPhamHopdong_Tab1("DonGia", i), DataGridViewComboBoxCell)
            '            comboxPrice.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
            '            comboxPrice.DataSource = listPrice
            '            comboxPrice.Value = dtSanPhamHD.Rows(i)("DonGia")
            '            dgvSanPhamHopdong_Tab1("SoLuong", i).Value = dtSanPhamHD.Rows(i)("SoLuong")
            '            If comboTien.SelectedValue = "VND" Then
            '                dgvSanPhamHopdong_Tab1("ThanhTien", i).Value = LamTron(dtSanPhamHD.Rows(i)("ThanhTien"), 0).ToString("N02", US)
            '            ElseIf comboTien.SelectedValue = "USD" Or comboTien.SelectedValue = "SGD" Then
            '                dgvSanPhamHopdong_Tab1("ThanhTien", i).Value = LamTron(dtSanPhamHD.Rows(i)("ThanhTien"), 2).ToString("N02", US)
            '            End If
            '            dgvSanPhamHopdong_Tab1("LoaiPhi", i).Value = dtSanPhamHD.Rows(i)("LoaiPhi")
            '            dgvSanPhamHopdong_Tab1("IsManyPrice", i).Value = dtSanPhamHD.Rows(i)("IsManyPrice")
            '            dgvSanPhamHopdong_Tab1.Rows(i).DefaultCellStyle.BackColor = Color.Aquamarine
            '        Else
            '            listPrice = New List(Of String)
            '            listPrice.Add(dtSanPhamHD.Rows(i)("DonGia"))
            '            comboxPrice = CType(dgvSanPhamHopdong_Tab1("DonGia", i), DataGridViewComboBoxCell)
            '            comboxPrice.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
            '            comboxPrice.DataSource = listPrice
            '            comboxPrice.Value = dtSanPhamHD.Rows(i)("DonGia")
            '            dgvSanPhamHopdong_Tab1("SoLuong", i).Value = dtSanPhamHD.Rows(i)("SoLuong")
            '            If comboTien.SelectedValue = "VND" Then
            '                dgvSanPhamHopdong_Tab1("ThanhTien", i).Value = LamTron(dtSanPhamHD.Rows(i)("ThanhTien"), 0).ToString("N02", US)
            '            ElseIf comboTien.SelectedValue = "USD" Or comboTien.SelectedValue = "SGD" Then
            '                dgvSanPhamHopdong_Tab1("ThanhTien", i).Value = LamTron(dtSanPhamHD.Rows(i)("ThanhTien"), 2).ToString("N02", US)
            '            End If
            '            dgvSanPhamHopdong_Tab1("LoaiPhi", i).Value = dtSanPhamHD.Rows(i)("LoaiPhi")
            '            dgvSanPhamHopdong_Tab1("IsManyPrice", i).Value = dtSanPhamHD.Rows(i)("IsManyPrice")
            '            dgvSanPhamHopdong_Tab1.Rows(i).DefaultCellStyle.BackColor = Color.White
            '        End If
            '    Else
            '        listPrice = New List(Of String)
            '        listPrice.Add("")
            '        comboxPrice = CType(dgvSanPhamHopdong_Tab1("DonGia", i), DataGridViewComboBoxCell)
            '        comboxPrice.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing
            '        comboxPrice.DataSource = listPrice
            '        comboxPrice.Value = ""
            '        dgvSanPhamHopdong_Tab1("SoLuong", i).Value = ""
            '        dgvSanPhamHopdong_Tab1("ThanhTien", i).Value = ""
            '        dgvSanPhamHopdong_Tab1("LoaiPhi", i).Value = dtSanPhamHD.Rows(i)("LoaiPhi")
            '        dgvSanPhamHopdong_Tab1("IsManyPrice", i).Value = dtSanPhamHD.Rows(i)("IsManyPrice")
            '    End If
            'Next
            dbConn.Close()
            TinhTienHangChuaThue()
            TinhTienThueVAT()
            TinhTienThueDaThue()
        Catch ex As Exception

        End Try
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Tạo số hóa đơn
    ''' </summary>
    ''' <remarks>
    ''' 
    ''' </remarks>
    ''' <history>
    '''   [lamtn] 19/02/2011  Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub TinhTienHangChuaThue()
        Try
            'Dim ThanhTienHangHoa As Integer = 0
            'fix VND,USD,SGD 
            Dim ThanhTienHangHoa As Double
            For i As Integer = 0 To dgvSanPhamHopdong_Tab1.Rows.Count - 1
                Dim IDSanPham As Integer = Integer.Parse(dgvSanPhamHopdong_Tab1("IDSanPham", i).Value)
                Dim LoaiPhi As String = dgvSanPhamHopdong_Tab1("LoaiPhi", i).Value
                'Dim iThanhTien As Integer
                Dim iThanhTien As Double
                If IDSanPham > 0 And LoaiPhi = "HangHoa" Then
                    iThanhTien = dgvSanPhamHopdong_Tab1("ThanhTien", i).Value
                    ThanhTienHangHoa = ThanhTienHangHoa + iThanhTien
                End If
            Next
            'fix VND,USD,SGD 
            'Dim ThanhTienGiamTru As Integer = 0
            Dim ThanhTienGiamTru As Double = 0
            For i As Integer = 0 To dgvSanPhamHopdong_Tab1.Rows.Count - 1
                Dim IDSanPham As Integer = Integer.Parse(dgvSanPhamHopdong_Tab1("IDSanPham", i).Value)
                Dim LoaiPhi As String = dgvSanPhamHopdong_Tab1("LoaiPhi", i).Value
                'fix VND,USD,SGD 
                'Dim iThanhTien As Integer
                Dim iThanhTien As Double
                If IDSanPham > 0 And LoaiPhi = "GiamTru" Then
                    iThanhTien = dgvSanPhamHopdong_Tab1("ThanhTien", i).Value
                    ThanhTienGiamTru = ThanhTienGiamTru + iThanhTien
                End If
            Next
            'Dim CongTienHang As Integer = ThanhTienHangHoa - ThanhTienGiamTru
            'fix VND,USD,SGD 
            Dim CongTienHang As Double = ThanhTienHangHoa - ThanhTienGiamTru
            txtCongTienHang.Text = CongTienHang.ToString("N02", US)
        Catch ex As Exception
            Throw
        End Try
    End Sub
    Private Sub TinhTienThueVAT()
        Try
            Dim TienThueVAT As Double
            Dim CongTienHang As Double
            Dim VAT As Double = txtVat.Value
            If txtCongTienHang.Text = "" Then
                CongTienHang = 0
            Else
                CongTienHang = Double.Parse(txtCongTienHang.Text)
            End If
            If comboTien.SelectedValue = "VND" Then
                TienThueVAT = LamTron((CongTienHang * VAT) / 100, 0)
            ElseIf comboTien.SelectedValue = "USD" Or comboTien.SelectedValue = "SGD" Then
                TienThueVAT = LamTron((CongTienHang * VAT) / 100, 2)
            End If
            txtTienThueVAT.Text = TienThueVAT.ToString("N02", US)
        Catch ex As Exception

        End Try
    End Sub
    Private Sub TinhTienThueDaThue()
        Try
            Dim TienThueVAT As Double
            Dim CongTienHang As Double
            Dim TongThanhToan As Double
            If txtCongTienHang.Text = "" Then
                CongTienHang = 0
            Else
                CongTienHang = Double.Parse(txtCongTienHang.Text)
            End If
            If txtTienThueVAT.Text = "" Then
                TienThueVAT = 0
            Else
                TienThueVAT = Double.Parse(txtTienThueVAT.Text)
            End If
            TongThanhToan = TienThueVAT + CongTienHang
            If comboTien.SelectedValue = "VND" Then
                TongThanhToan = LamTron(TongThanhToan)
            ElseIf comboTien.SelectedValue = "USD" Or comboTien.SelectedValue = "SGD" Then
                TongThanhToan = LamTron(TongThanhToan, 2)
            End If
            txtTongThanhToan.Text = TongThanhToan.ToString("N02", US)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        Me.Close()
    End Sub
    ''' <summary>
    ''' Chuyển thông tin từ đơn hàng sang phiếu nhập kho
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmdChuyenPhieuKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChuyenPhieuKho.Click
        Try
            Dim PhieuNhapKho As New frmThemPhieuNhapKho
            PhieuNhapKho.FormSender = "XemDonHang"
            PhieuNhapKho.Ngay = dtpNgayHopDong.Value
            PhieuNhapKho.Cua = txtTenKhachHang.Text
            PhieuNhapKho.KhachHangID = KhachHangID
            PhieuNhapKho.So = Integer.Parse(txtSo.Text)
            PhieuNhapKho.dtChiTietDonHang = dtChiTietDonHang
            ShowModalForm(PhieuNhapKho, False, False)
        Catch ex As Exception

        End Try
    End Sub
End Class